package com.hubu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hubu.entity.HldQuestion;
import com.hubu.entity.HldTestResult;
import com.hubu.entity.HldUsernameInformation;
import com.hubu.entity.vo.HldQuesVO;
import com.hubu.entity.vo.HldResultVO;
import com.hubu.mapper.HldQuestionMapper;
import com.hubu.service.HldQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hubu.service.HldTestResultService;
import com.hubu.service.HldUsernameInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Hubu
 * @since 2021-11-18
 */
@Service
public class HldQuestionServiceImpl extends ServiceImpl<HldQuestionMapper, HldQuestion> implements HldQuestionService {

    @Autowired
    HldUsernameInformationService hldUsernameInformationService;

    @Autowired
    HldTestResultService hldTestResultService;

    @Override
    public  Map<String, Object> submitHldTest(HldResultVO hldResultVO) {
        // 6种人格得分初始化
        StringBuilder resultXG = new StringBuilder();
        Map<String, Integer> resultCountMap = new HashMap<>();
        resultCountMap.put("R", 0);
        resultCountMap.put("I", 0);
        resultCountMap.put("A", 0);
        resultCountMap.put("S", 0);
        resultCountMap.put("E", 0);
        resultCountMap.put("C", 0);

        // 1. 处理分数匹配
        List<HldQuesVO> resultList = hldResultVO.getResultList();
        Integer tempCount = 0;
        for (HldQuesVO hldQuesVO : resultList) {
            QueryWrapper<HldQuestion> wrapper = new QueryWrapper<>();
            wrapper.eq("id", hldQuesVO.getQuestionId());
            wrapper.eq("dn", hldQuesVO.getAnswer());


            HldQuestion hldQuestion = getOne(wrapper);
            if (hldQuestion != null) {
                tempCount = resultCountMap.get(hldQuestion.getXglx());
                resultCountMap.replace(hldQuestion.getXglx(), ++tempCount);
            }
        }


        // 2. 选择最高的三种人格
        List<Map.Entry<String, Integer>> list = new ArrayList(resultCountMap.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        });


        Iterator<Map.Entry<String, Integer>> iter = list.iterator();
        for (int i = 0; i < 3 && iter.hasNext(); i++) {
            Map.Entry<String, Integer> item = iter.next();
            String key = item.getKey();
            resultXG.append(key);
        }

        System.out.println("resultXG=>" +  resultXG);

        // 3. 找到性格合适的专业
        QueryWrapper<HldTestResult> HldTestResultWrapper = new QueryWrapper<>();
        HldTestResultWrapper.eq("jg", String.valueOf(resultXG));
        HldTestResult hldTestResult = hldTestResultService.getOne(HldTestResultWrapper);

        // 4. 保存每个人的性格
        HldUsernameInformation hldUsernameInformation = new HldUsernameInformation();
        hldUsernameInformation.setUsername("xgk");
        hldUsernameInformation.setDyzy(hldTestResult.getDyzy());
        hldUsernameInformation.setXglxResult(String.valueOf(resultXG));
        hldUsernameInformationService.save(hldUsernameInformation);

        Map<String, Object> result = new HashMap<>();
        result.put("resultCountMap", resultCountMap);
        result.put("hldTestResult", hldTestResult);
        // 5. 返回数据
        return result;
    }
}
